/** 节流函数 */
export const throttle = (fn, dealy = 30) => {
  let timer = null

  return function (...args) {
    const context = this

    if (!timer) {
      timer = setTimeout(() => {
        fn.apply(context, args)
        clearTimeout(timer)

        timer = null
      }, dealy)
    }
  }
}

/** 防抖 */
export const debounce = (fn, dealy = 30) => {
  let timer = null

  return function (...args) {
    const context = this

    if (timer) clearTimeout(timer)

    timer = setTimeout(() => {
      fn.apply(context, args)
    }, dealy)
  }
}
